unit TimeHelpers;

interface

function TimeInPeriod(ATime, BeginPeriodTime, EndPeriodTime: TDateTime): Boolean;

implementation

uses
 SysUtils,
 DateUtils;

function TimeInPeriod(ATime, BeginPeriodTime, EndPeriodTime: TDateTime): Boolean;
 var
  Midnight: TDateTime;
begin
 Result:=false;
 Midnight:=StrToTime('00:00');
 case CompareTime(BeginPeriodTime, Midnight) of
  0: //BPT = 0
   begin
    case CompareTime(EndPeriodTime, Midnight) of
     0: //EPT = 0
      begin
       Result:=CompareTime(ATime, Midnight)=0;
      end;
     1: //EPT > 0
      begin
       Result:=CompareTime(ATime, EndPeriodTime)<=0;
      end;
     end;
   end;
  1: //BPT > 0
   begin
    case CompareTime(EndPeriodTime, BeginPeriodTime) of
     0: //EPT = BPT
      begin
       Result:=CompareTime(ATime, BeginPeriodTime)=0;
      end;
     -1 {EPT < BPT -> BPT - 0 - EPT}:
      begin
       Result:=(CompareTime(ATime, BeginPeriodTime)>=0) or (CompareTime(ATime, EndPeriodTime)<=0);
      end;
     1: //EPT > BPT
      begin
       Result:=(CompareTime(ATime, BeginPeriodTime)>=0) and (CompareTime(ATime, EndPeriodTime)<=0);
      end;
     end;
   end;
  end;
end;

end.
 